<?php

require_once(Yii::getPathOfAlias('webroot')."/generate.php");

class ThemesController extends Controller
{
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
	public function allowedActions()
	{
		//return 'error';
	}
	
	public function actionIndex()
	{
		$model = new Themes('search');
		$model->unsetAttributes();  // clear any default values
		
		if(isset($_GET['Themes']))
			$model->attributes=$_GET['Themes'];

		$this->render('index',array(
			'model'=>$model,
		));
	}
	public function actionCreate()
	{
		$model = new Themes;
		$generate = new generate;
		$cateTheme = new CategoriesTheme;
		
		if(!empty($_POST['Themes'])){
	
			// lay du lieu 
			$model->attributes = $_POST['Themes'];
			$model->alias = alias($_POST['Themes']['name']);
			$model->created = time();
			$model->alias_folder = alias_change($_POST['Themes']['alias_folder']);
			if(!empty(CUploadedFile::getInstance($model,'image')->name))
				$model->image = $generate->getMd5NameFile(CUploadedFile::getInstance($model,'image'));
			if(!empty(CUploadedFile::getInstance($model,'image_detail')->name))
				$model->image_detail = $generate->getMd5NameFile(CUploadedFile::getInstance($model,'image_detail'));
			
			//luu du lieu
			if($model->save()){
				// luu hinh anh
				$path = Yii::getPathOfAlias('webroot').'/upload/images/';
				if(!empty(CUploadedFile::getInstance($model,'image')->name))
					CUploadedFile::getInstance($model,'image')->saveAs($path.$model->image);
				if(!empty(CUploadedFile::getInstance($model,'image_detail')->name))
					CUploadedFile::getInstance($model,'image_detail')->saveAs($path.$model->image_detail);
				$alias_cate = $cateTheme->getAliasFolder($model->id_category_theme);
				// luu file sql 
				if(!empty(CUploadedFile::getInstance($model,'fileSql')->name)){
					$file = CUploadedFile::getInstance($model,'fileSql');
					$path = "/shop/".$alias_cate."/db/";
					$nameNew = $alias_cate."_".$model->alias_folder;
					$generate->uploadFile($file, $path, $nameNew);
				}
				// luu source theme
				if(!empty(CUploadedFile::getInstance($model,'theme')->name)){
					$file = CUploadedFile::getInstance($model,'theme');
					$path = "/shop/".$alias_cate."/theme/";
					$nameNew = $alias_cate."_".$model->alias_folder;
					$generate->uploadFile($file, $path, $nameNew);
					// giai nen source theme .zip
					$generate->extractFileZip($path.$nameNew.".zip", $path.$nameNew);
					// remove source theme .zip
					$generate->removeFile("/../shop/".$alias_cate."/theme/".$nameNew.".zip");
				}
				Yii::app()->user->setFlash('success', translate('Thêm theme thành công.'));
				$this->redirect(PIUrl::createUrl('/admin/themes'));
				
			}
		}
		
		$dataCategories = CategoriesTheme::model()->getDataCategories();
		
		$this->render('create', array(
			"model" => $model,
			"dataCategories" => $dataCategories,
		));
	}
	
	public function actionUpdate($id)
	{
		$model = Themes::model()->findByPk($id);
		$generate = new generate;
		$cateTheme = new CategoriesTheme;
		
		if(!empty($_POST['Themes'])){
	
			$image_old = $model->image; $flag_image = 0;
			$image_detail_old = $model->image_detail; $flag_image_detail = 0;
			$alias_folder_old = $model->alias_folder;

			// lay du lieu 
			$model->attributes = $_POST['Themes'];
			$model->alias = alias($_POST['Themes']['name']);
			$model->updated = time();
			$model->alias_folder = alias_change($_POST['Themes']['alias_folder']);
			if(!empty(CUploadedFile::getInstance($model,'image')->name)){
				$model->image = $generate->getMd5NameFile(CUploadedFile::getInstance($model,'image'));
				if(!empty($image_old))
					$generate->removeFile("/../upload/images/".$image_old);
				$flag_image = 1;
			}
			else{
				$model->image = $image_old;
			}
			if(!empty(CUploadedFile::getInstance($model,'image_detail')->name)){
				$model->image_detail = $generate->getMd5NameFile(CUploadedFile::getInstance($model,'image_detail'));
				if(!empty($image_detail_old))
					$generate->removeFile("/../upload/images/".$image_detail_old);
				$flag_image_detail = 1;
			}
			else{
				$model->image_detail = $image_detail_old;
			}
			
			//luu du lieu
			if($model->save()){
				// luu hinh anh
				$path = Yii::getPathOfAlias('webroot').'/upload/images/';
				if($flag_image == 1)
					CUploadedFile::getInstance($model,'image')->saveAs($path.$model->image);
				if($flag_image_detail == 1)
					CUploadedFile::getInstance($model,'image_detail')->saveAs($path.$model->image_detail);
				$alias_cate = $cateTheme->getAliasFolder($model->id_category_theme);
				// luu file sql 
				if(!empty(CUploadedFile::getInstance($model,'fileSql')->name)){
					$file = CUploadedFile::getInstance($model,'fileSql');
					$path = "/shop/".$alias_cate."/db/";
					$nameNew = $alias_cate."_".$model->alias_folder;
					// remove file sql old
					$generate->removeFile("/../".$path.$alias_cate."_".$alias_folder_old.".sql");
					
					// upload file sql new
					$generate->uploadFile($file, $path, $nameNew);
				}else{
					// if alias folder is change , rename file sql
					if($alias_folder_old != $model->alias_folder){
						$path = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/db/".$alias_cate."_".$alias_folder_old.".sql";
						$pathNew = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/db/".$alias_cate."_".$model->alias_folder.".sql";
						rename($path, $pathNew);
					}
				}
				// luu source theme
				if(!empty(CUploadedFile::getInstance($model,'theme')->name)){
					
					$file = CUploadedFile::getInstance($model,'theme');
					$path = "/shop/".$alias_cate."/theme/";
					$nameNew = $alias_cate."_".$model->alias_folder;
					
					// remove folder source old
					if(is_dir(Yii::getPathOfAlias('webroot').$path.$alias_cate."_".$alias_folder_old))
						$generate->rrmdir(Yii::getPathOfAlias('webroot').$path.$alias_cate."_".$alias_folder_old);
					
					// upload folder source new
					$generate->uploadFile($file, $path, $nameNew);
					
					// giai nen source theme .zip
					$generate->extractFileZip($path.$nameNew.".zip", $path.$nameNew);
					
					// remove source theme .zip new
					$generate->removeFile("/../shop/".$alias_cate."/theme/".$nameNew.".zip");
				}else{
					// if alias folder is change , rename file .zip and folder source
					if($alias_folder_old != $model->alias_folder){
						$path = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/theme/".$alias_cate."_".$alias_folder_old;
						$pathNew = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/theme/".$alias_cate."_".$model->alias_folder;
						rename($path, $pathNew);
					}
				}
				Yii::app()->user->setFlash('success', translate('Sửa 	theme thành công.'));
				$this->redirect(PIUrl::createUrl('/admin/themes'));
				
			}
		}
		
		$dataCategories = CategoriesTheme::model()->getDataCategories();
		
		$this->render('update', array(
			"model" => $model,
			"dataCategories" => $dataCategories,
		));
	}
	
	public function loadModel($id)
	{
		$model=Themes::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	public function actionDelete($id)
	{
		$generate = new generate;
		$cateTheme = new CategoriesTheme;
		$model = $this->loadModel($id);
		$alias_cate = $cateTheme->getAliasFolder($model->id_category_theme);
		// remove image and image detail
		if(!empty($model->image))
			$generate->removeFile("/../upload/images/".$model->image);
		if(!empty($model->image_detail))
			$generate->removeFile("/../upload/images/".$model->image_detail);
		// remove file sql
		if(!empty($model->alias_folder))
			$generate->removeFile("/../shop/".$alias_cate."/db/".$alias_cate."_".$model->alias_folder.".sql");
		
		// remove folder source
		$path = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/theme/".$alias_cate."_".$model->alias_folder;
		if(is_dir($path)){
			$generate->rrmdir($path);
		}
	
		$model->delete();
		
		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
	
	// function delete rows
	public function actionDeleteAll($id){
		$arrIdNew = explode(",",$id);
		$cateTheme = new CategoriesTheme;
		$generate = new generate;
		
		for($i=0; $i<count($arrIdNew); $i++){
			$model = $this->loadModel($arrIdNew[$i]);
			$alias_cate = $cateTheme->getAliasFolder($model->id_category_theme);
			// remove image and image detail
			if(!empty($model->image))
				$generate->removeFile("/../upload/images/".$model->image);
			if(!empty($model->image_detail))
			$generate->removeFile("/../upload/images/".$model->image_detail);
			
			// remove file sql
			if(!empty($model->alias_folder))
				$generate->removeFile("/../shop/".$alias_cate."/db/".$alias_cate."_".$model->alias_folder.".sql");
			
			// remove folder source
			$path = Yii::getPathOfAlias('webroot')."/shop/".$alias_cate."/theme/".$alias_cate."_".$model->alias_folder;
			if(is_dir($path)){
				$generate->rrmdir($path);
			}
			
			$model->delete();
		}
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
	}
}